package com.electric.qrapp.utils.view

import android.animation.*
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import kotlin.random.Random


/**
 *@author created by WA03675
 *@date  2021-09-07 14:04
 *@des
 */
class NewLoadingView(context: Context, attr: AttributeSet? = null) : View(context, attr) {
    private val radian =  150f
    private val totalTime = 1000L
    private val outPaintStork = 60f
    private val innerPaintStork = outPaintStork * 1.6f
    private val intervalRadian = 0
    private var progress = 0f
    private val animatorSet: AnimatorSet = AnimatorSet()
    private val random = Random(256)


    private val valueAnimator: ValueAnimator by lazy {
        ValueAnimator.ofFloat(0f, 1f ).apply {
            duration = totalTime
            repeatMode = ValueAnimator.REVERSE
            repeatCount = ValueAnimator.INFINITE
        }
    }

    //旋转
    private val rotationAnimator: ObjectAnimator by lazy {
        val rotationAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);
        rotationAnimator.duration = totalTime
        rotationAnimator.interpolator = LinearInterpolator();
        rotationAnimator.repeatCount = ValueAnimator.INFINITE;
        rotationAnimator.repeatMode = ValueAnimator.RESTART;
        return@lazy rotationAnimator
    }


    private val outRectF: RectF by lazy {
        getRectF(outPaintStork)
    }


    private val paint: Paint  = getPaint()



    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        //外圈 第一个圆弧
        canvas!!.drawArc(outRectF, 270f + (intervalRadian shr 1), radian * progress, false, paint)
        //外圈 第二个圆弧
        canvas.drawArc(outRectF, 90f + (intervalRadian shr 1), radian * progress, false, paint)

    }




    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        startAnim()
    }

    private fun startAnim() {
        if (!isRunning()) {
            valueAnimator.addUpdateListener {
                progress = it.animatedValue as Float
                if (progress<=0.02){
                    paint.color = randomColor(random)
                }
                postInvalidate()
            }

            valueAnimator.start()

            animatorSet.playTogether(
                rotationAnimator
            )


            animatorSet.start()

        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (isRunning()) {
            animatorSet.cancel()
            animatorSet.removeAllListeners()

            valueAnimator.cancel()
            valueAnimator.removeAllListeners()

        }
    }

    private fun isRunning(): Boolean = valueAnimator.isRunning


    private fun getRectF(paintStork:Float):RectF{
       return RectF(paintStork, paintStork, width - paintStork, height - paintStork)
    }

    private fun getPaint():Paint{
       return Paint().apply {
            isAntiAlias = true
            color = randomColor(random)
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = 14f
        }
    }

   private fun randomColor(random:Random): Int {
        val red: Int = random.nextInt(256)
        val green: Int = random.nextInt(256)
        val blue: Int = random.nextInt(256)
        return Color.rgb(red, green, blue)
    }

}